package edu.qd.learn.newchapter.chapter04;


import java.util.*;

/**
 * 练习题讲解
 *
 * @author 布衣
 */
public class Question01 {

    /**
     * this is my better.
     *
     * @param str
     * @return
     */
    public static String firstWordToUpper(String str) {
        String reStr = "";
        int pre = 0;
        for (int i = 0; i != -1; i = str.indexOf(" ", i + 1)) {
            if (i == 0) {
                pre++;
                reStr += String.valueOf(str.charAt(i)).toUpperCase();
            } else {
                //substring (>=pre,<i+1)
                reStr += str.substring(pre, i + 1);
                if (i != str.length() - 1) {
                    reStr += String.valueOf(str.charAt(i + 1)).toUpperCase();
                }
                pre = i + 2;
            }
        }
        if (pre < str.length()) {
            reStr += str.substring(pre);
        }
        return reStr;
    }

    public static String firstWordToUpper01(String str) {
        String reStr = "";
        String[] split = str.split(" ");
        for (String s : split) {
//            if (s != null && !s.isEmpty()) {
//                reStr += s.substring(0, 1).toUpperCase() + s.substring(1)+" ";
//            } else {
//                reStr += " ";
//            }
            reStr += (s != null && !s.isEmpty()) ? s.substring(0, 1).toUpperCase() + s.substring(1) + " " : " ";
        }
        return reStr;
    }

    public static String firstWordToUpper02(String str) {
        char[] charArray = str.toCharArray();
        for (int i = 0; i != -1; i = str.indexOf(" ", i + 1)) {
            if (i == 0) {
                charArray[i] = String.valueOf(charArray[i]).toUpperCase().charAt(0);
            } else {
                if (i != str.length() - 1) {
                    charArray[i + 1] = String.valueOf(charArray[i + 1]).toUpperCase().charAt(0);
                }
            }
        }
        return new String(charArray);
    }


    //设计一个方法，将给定字符串的正序和反序进行连接 ok—>okkook
    public static String myJoin(String wd, int len) {
        if (len < wd.length()) {
            throw new RuntimeException("给定的长度不能小于字符串长度");
        }
        String reStr = "";
        String reverseStr = "";
        for (int i = wd.length() - 1; i >= 0; i--) {
            reverseStr += wd.charAt(i);
        }

        int i = len / wd.length() + (len % wd.length() > 0 ? 1 : 0);
        for(int i1 = 0;i1 < i;i1++){
            if(i1 % 2 ==0){
                reStr += wd;
            }else{
                reStr += reverseStr;
            }
        }
        return reStr.substring(0,len);
    }

    public static int myStrLen(char  str){
        String c = String.valueOf(str);
        return c.getBytes().length;
    }

    //Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak

    public static String findTwo(String str,String lastStr) {
        int begin = str.lastIndexOf(lastStr);
        String beginStr = str.substring(0, begin);
        String endStr =  str.substring(begin+1);
        return beginStr+String.valueOf(str.charAt(begin)).toUpperCase()+endStr;
    }

    public static void main(String[] args) {
        System.out.println(findTwo("Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak", "two"));
        System.out.println(myJoin("oka", 100));
        System.out.println(myStrLen('中'));

        System.out.println(firstWordToUpper02("this      Is my better. "));

//        String str = "this is my better. are you ok?";
//
//        for (int i = 0; i != -1; i = str.indexOf(" ", i + 1)) {
//            if (i == 0) {
//                System.out.println(str.charAt(i));
//            } else {
//                System.out.println(str.charAt(i + 1));
//            }
//
//        }
//
//        int first = str.indexOf(" ");
//        System.out.println(first);
//        int second = str.indexOf(" ", first + 1);
//        System.out.println(second);
//
//        int third = str.indexOf(" ", second + 1);
//        System.out.println(third);
//
//        int fourth = str.indexOf(" ", third + 1);
//        System.out.println(fourth);
    }



    //有一个学生数组 ，学生信息包含(学号,成绩)
    //1. 列出所有成绩大于60的学生信息 ，用两种方法实现，lambda
    //2.将数组里面的成绩小于60的删除
    //3. 按成绩排序
}
